package com.jarveis.frame.redis.mq;

import org.apache.log4j.Logger;

import com.jarveis.frame.redis.RedisManager;

import redis.clients.jedis.Jedis;

/**
 * @desc 消息消费者
 * @author liuguojun
 * @date 2016-01-14
 */
public class RMQConsumer {

	private static final Logger logger = Logger.getLogger(RMQConsumer.class);
	private RMQQueue queue;

	/**
	 * 构造方法
	 * 
	 * @param jedis
	 * @param queue
	 * @throws Exception
	 */
	public RMQConsumer(final Jedis jedis, final String queue) throws Exception {
		if (jedis == null) {
			logger.info("jedis未成功连接");
			throw new Exception("jedis未成功连接");
		}
		this.queue = new RMQQueue(queue, jedis);
	}

	/**
	 * 构造方法
	 * 
	 * @param queue
	 * @throws Exception
	 */
	public RMQConsumer(final String queue) throws Exception {
		Jedis jedis = RedisManager.getConnection();
		if (jedis == null) {
			logger.info("jedis未成功连接");
			throw new Exception("jedis未成功连接");
		}
		this.queue = new RMQQueue(queue, jedis);
	}

	/**
	 * 消费消息
	 * 
	 * @param callback
	 */
	public void consume(RMQCallback callback) {
		queue.consume(callback);
	}

	/**
	 * 消费消息
	 * 
	 * @return
	 */
	public String consume() {
		return queue.consume();
	}

	/**
	 * 获取未读的消息数
	 * 
	 * @return
	 */
	public long count() {
		return queue.count();
	}
}